import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/exercise_plan_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/drill/common/drill_common_widget.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/task_emergency/task_drill_plan/task_drill_plan_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

/// 我的任务-应急演练计划
class TaskDrillPlanPage extends StatelessWidget {
  TaskDrillPlanPage({super.key});

  final controller = Get.put(TaskDrillPlanController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          appBar: CustomAppBar(titleText: '演练计划'),
          key: controller.popRightScaffoldKey,
          endDrawer: Container(
            width: AppDimens.sw - 75.w,
            height: AppDimens.sh,
            color: Colors.white,
            child: _getFilterWidget(),
          ),
          body: Container(
            color: const Color(0xffF5F5F6),
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
            child: Column(children: [
              _getSearchConditionWidget(),
              AppGaps.vGap12,
              Expanded(
                  child: CustomEasyRefreshList<ExercisePlanEntity>(
                key: controller.globalKey,
                dataLoad: (pageIndex, pageSize, {String? search}) async {
                  return controller.getListData(pageIndex, pageSize);
                },
                itemBuilder: (ExercisePlanEntity item, int index) {
                  return CustomTitleChartPage(
                    title: item.exercisePlanName,
                    titleRightWidget: DrillPlanCommonWidget().getStateView(item.toExamineStatus),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        CustomTextWidget.getTextRowView(title: '演练类别：', value: item.exercisePlanClassName),
                        AppGaps.vGap12,
                        CustomTextWidget.getTextRowView(title: '演练类型：', value: item.exercisePlanTypeName),
                        AppGaps.vGap12,
                        CustomTextWidget.getTextRowView(title: '预案名称：', value: item.emergencyIdName),
                        _getApprovalButton(item)
                      ],
                    ),
                  ).clickOnce(() {
                    Get.toNamed(AppRoutes.drillDetails, arguments: {'type': 1, 'id': item.id});
                  });
                },
              ))
            ]),
          ),
        );
      },
    );
  }

  /// 获取右侧筛选样式
  Widget _getFilterWidget() {
    return CustomFilterRightSheet(
      dataList: controller.allFilterList,
      onConfirm: controller.screenConfirm,
      isRadio: true,
    );
  }

  /// 获取审批按钮
  Widget _getApprovalButton(ExercisePlanEntity item) {
    String buttonText = '';
    if (item.toExamineStatus == 2 || item.toExamineStatus == 3) {
      buttonText = '审批';
    } else if (item.toExamineStatus == 8) {
      buttonText = '进入';
    }
    if (item.toExamineStatus == 8 && item.exercisePlanTypeName == '桌面演练') {
      return Container();
    }
    return Visibility(
      visible: buttonText.isNotEmpty,
      child: Column(
        children: [
          AppGaps.vGap12,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '操作:',
                style: TextStyle(
                  fontSize: 13.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xffA5A6B5),
                ),
              ),
              Container(
                padding: EdgeInsets.fromLTRB(10.w, 6.w, 6.w, 6.w),
                decoration: BoxDecoration(
                  color: const Color(0xFFE5E7FF),
                  borderRadius: BorderRadius.circular(4.w),
                ),
                child: Row(
                  children: [
                    Text(
                      buttonText,
                      style: TextStyle(
                        fontSize: 13.sp,
                        height: 1.2,
                        fontWeight: FontWeight.w400,
                        color: const Color(0xff5C6DFF),
                      ),
                    ),
                    AppGaps.hGap2,
                    SvgPicture.asset(
                      AppImages.icon_right_arrow,
                    ),
                  ],
                ),
              ).clickOnce(() {
                switch (buttonText) {
                  case '审批':
                    controller.clickOnApproval(item);
                    return;
                  case '进入':
                    Get.toNamed(AppRoutes.drillPlanExecute, arguments: item.id);
                    return;
                }
              }),
            ],
          ),
        ],
      ),
    );
  }

  ///获取条件搜索项
  Widget _getSearchConditionWidget() {
    return Row(
      children: [
        Expanded(
            child: CustomSearchEnter(
          height: 32.w,
          hintText: '请输入名称',
          text: controller.search,
          onCallBack: (value) {
            controller.search = value;
            controller.refreshPage();
          },
        )),
        AppGaps.hGap8,
        _screen(),
      ],
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 32.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 18.w,
        height: 16.w,
      ),
    ).clickOnce(controller.screen);
  }
}
